import { Module } from 'vuex'
import { ShippingState, RootState } from '../types'
import { ShippingService } from '@/api/shipping'

const shippingModule: Module<ShippingState, RootState> = {
  namespaced: true,
  
  state: {
    loading: false,
    currentTracking: null
  },
  
  mutations: {
    SET_LOADING(state, loading: boolean) {
      state.loading = loading
    },
    SET_CURRENT_TRACKING(state, tracking) {
      state.currentTracking = tracking
    }
  },
  
  actions: {
    async getShippingTracking({ commit }, orderId) {
      commit('SET_LOADING', true)
      try {
        const response = await ShippingService.getShippingTracking(orderId)
        commit('SET_CURRENT_TRACKING', response.data)
        return response.data
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async updateShippingStatus({ dispatch }, { orderId, ...data }) {
      await ShippingService.updateShippingStatus(orderId, data)
      return dispatch('getShippingTracking', orderId)
    }
  }
}

export default shippingModule 